package cn.edu.sjtu.se.dslab.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.sjtu.se.dslab.component.method.MethodComponent;
import cn.edu.sjtu.se.dslab.dao.IMethodComponentDao;
import cn.edu.sjtu.se.dslab.dao.IProjectDao;
import cn.edu.sjtu.se.dslab.dao.IProjectMethodDao;
import cn.edu.sjtu.se.dslab.project.Project;
import cn.edu.sjtu.se.dslab.project.ProjectMethod;
import cn.edu.sjtu.se.dslab.service.IProjectMethodService;

@Service("projectMethodService")
@Transactional
public class ProjectMethodServiceImpl implements IProjectMethodService {
	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Resource(name = "projectMethodDao")
	private IProjectMethodDao projectMethodDao;
	
	@Resource(name = "projectDao")
	private IProjectDao projectDao;
	
	@Resource(name = "methodComponentDao")
	private IMethodComponentDao methodComponentDao;
	
	@Override
	public boolean addProjectMethod(ProjectMethod projectMethod) {
		projectMethodDao.save(projectMethod);
		return true;
	}
	
	@Override
	public boolean addProjectMethods(long projectId, Long[] methodComponentIds){
		Project project = projectDao.queryById(projectId);
		if (project == null) throw new NullPointerException("Project Id not exist!: " + projectId);
		List<ProjectMethod> projectMethods = projectMethodDao.getListByProject(project);
		Map<Long, ProjectMethod> projectMethodMapping = new HashMap<Long, ProjectMethod>();
		for (ProjectMethod projectMethod : projectMethods){
			projectMethodMapping.put(projectMethod.getMethodComponent().getId(), projectMethod);
			//logger.info(projectMethod.getProject().getId()  + "---" + projectMethod.getUser().getId());
		}
		for (int i = 0; i< methodComponentIds.length; ++i){
			//更新原有的
			if (projectMethodMapping.containsKey(methodComponentIds[i])){
				projectMethodMapping.remove(methodComponentIds[i]);
			}else{
				ProjectMethod projectMethod = new ProjectMethod();
				projectMethod.setProject(projectDao.queryById(projectId));
				projectMethod.setMethodComponent(methodComponentDao.queryById(methodComponentIds[i]));
				projectMethodDao.save(projectMethod);
			}
		}
		//删除剩下的
		for(Entry<Long,ProjectMethod> entry: projectMethodMapping.entrySet()){
			logger.info("Delete methodComponent id:" + entry.getKey());
			projectMethodDao.delete(entry.getValue());
		}
		return true;
	}

	@Override
	public boolean deleteProjectMethodById(long id) {
		return projectMethodDao.deleteById(id);
	}

	@Override
	public boolean deleteProjectMethod(ProjectMethod projectMethod) {
		return projectMethodDao.delete(projectMethod);
	}

	@Override
	public List<ProjectMethod> getListByProject(Project project) {
		return projectMethodDao.getListByProject(project);
	}
	
	@Override
	public List<Project> getProjectListByMethodComponent(MethodComponent methodComponent) {
		List<ProjectMethod> projectMethods =  projectMethodDao.getListByMethodComponent(methodComponent);
		List<Project> projects = new ArrayList<Project>();
		for (ProjectMethod up : projectMethods){
			if (up.getProject().isDeleted()) continue;
			projects.add(up.getProject());
		}
		return projects;
	}

	@Override
	public boolean updateProjectMethod(ProjectMethod projectMethod) {
		return projectMethodDao.update(projectMethod);
	}

	@Override
	public boolean deleteProjectMethodByIds(Long[] projectMethodIds) {
		boolean result = true;
		for (Long id: projectMethodIds){
			if (!projectMethodDao.deleteById(id)) result = false;
		}
		return result;
	}

}
